package com.dengqi.mymodule.activity.indicator;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.widget.LinearLayout;

import com.dengqi.mymodule.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.WrapPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.IntDef;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

/**
 * Author: deng
 * Date: 2020/11/5
 * Description: https://github.com/hackware1993/MagicIndicator
 */
public class MagicIndicatorTools<T> {

    private Context mContext;
    private List<T> mList = null;
    private String[] mArrays = null;
    private boolean isAdjustMode;
    private boolean isSkimOver;
    private int indicatorStyle;
    private int textSize = 12;
    private ViewPager mViewPager;
    private OnClipPagerClickListener mOnClipPagerClickListener;
    private OnClipPagerTitleListener mOnClipPagerTitleListener;

//    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();

    public MagicIndicatorTools(Context context) {
        mContext = context;
    }

    public MagicIndicatorTools setArrays(String[] arrays) {
        this.mList= (List<T>) Arrays.asList(mArrays);
        this.mArrays = null;
        return this;
    }

    public MagicIndicatorTools setList(List<T> mList) {
        this.mList = mList;
        this.mArrays=null;
        return this;
    }

    public void setViewPager(ViewPager mViewPager) {
        this.mViewPager = mViewPager;
    }

    /**
     * 指示器风格
     */
    @IntDef({IndicatorStyle.NORMAL, MagicIndicatorTools.IndicatorStyle.BOX,
            MagicIndicatorTools.IndicatorStyle.LINE, IndicatorStyle.DOT, IndicatorStyle.BORDER})
    public @interface IndicatorStyle {
        int NORMAL = 0;  // 普通
        int BOX = 1;     // 盒子
        int LINE = 2;    // 线
        int DOT = 3;     // 点
        int BORDER = 4;     //边框
    }

    /**
     * 指示器显示风格
     *
     * @param indicatorStyle
     * @return
     */
    public MagicIndicatorTools setIndicatorStyle(@IndicatorStyle int indicatorStyle) {
        this.indicatorStyle = indicatorStyle;
        return this;
    }

    /**
     * 是否需要设置等分模式
     *
     * @param isAdjustMode
     * @return
     */
    public MagicIndicatorTools setAdjustMode(boolean isAdjustMode) {
        this.isAdjustMode = isAdjustMode;
        return this;
    }

    /**
     * 跨多页切换时，中间页是否显示 "掠过" 效果
     *
     * @param isSkimOver
     * @return
     */
    public MagicIndicatorTools setSkimOver(boolean isSkimOver) {
        this.isSkimOver = isSkimOver;
        return this;
    }

    public MagicIndicatorTools setTextSize(int textSize) {
        this.textSize = textSize;
        return this;
    }

    public MagicIndicatorTools builder(MagicIndicator magicIndicator) {
        CommonNavigator commonNavigator = new CommonNavigator(mContext);
        commonNavigator.setAdjustMode(isAdjustMode);
        commonNavigator.setSkimOver(isSkimOver);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mList == null ? mArrays.length : mList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(mContext, R.color.colorMagicText_n));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(mContext, R.color.colorMagicText_l));
                simplePagerTitleView.setTextSize(textSize);
                setText(simplePagerTitleView, mList.get(index));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                                mFragmentContainerHelper.handlePageSelected(index);
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                switch (indicatorStyle) {
                    case IndicatorStyle.BOX:
                        WrapPagerIndicator wrapPagerindicator = new WrapPagerIndicator(context);
                        wrapPagerindicator.setFillColor(ContextCompat.getColor(mContext, R.color.colorMagicIndicator_l));
                        return wrapPagerindicator;
                    case IndicatorStyle.LINE:
                        LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                        linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                        linePagerIndicator.setColors(ContextCompat.getColor(mContext, R.color.colorMagicIndicator_l));
                        return linePagerIndicator;
                    case IndicatorStyle.DOT:
                        BezierPagerIndicator bezierIndicator = new BezierPagerIndicator(context);
                        bezierIndicator.setColors(ContextCompat.getColor(mContext, R.color.colorMagicIndicator_l));
                        return bezierIndicator;
                    case IndicatorStyle.BORDER:
                        WrapPagerIndicator borderIndicator = new WrapPagerIndicator(context);
                        borderIndicator.getPaint().setStyle(Paint.Style.STROKE);
                        borderIndicator.setRoundRadius(10f);
                        borderIndicator.setFillColor(ContextCompat.getColor(mContext, R.color.colorMagicIndicator_l));
                        return borderIndicator;
                }
                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        if (!isAdjustMode) {
            LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
            titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        }
//        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
        return this;
    }

    public MagicIndicatorTools setOnClipPagerClickListener(OnClipPagerClickListener mOnClipPagerClickListener) {
        this.mOnClipPagerClickListener = mOnClipPagerClickListener;
        return this;
    }

    public interface OnClipPagerClickListener<T> {
        void onClipPager(View v, int position, T mode);
    }


    public MagicIndicatorTools setOnClipPagerTitleListener(OnClipPagerTitleListener mOnClipPagerClickListener) {
        this.mOnClipPagerTitleListener = mOnClipPagerClickListener;
        return this;
    }

    public interface OnClipPagerTitleListener {
        String setClipPagerTitle(int position);
    }

    private void setText(View textView, Object object){
        if(textView instanceof ClipPagerTitleView){
            if(object instanceof IIndicatorView){
                ((ClipPagerTitleView)textView).setText(((IIndicatorView) object).getTitleText());
            }else{
                ((ClipPagerTitleView)textView).setText(object.toString());
            }
        }else if(textView instanceof SimplePagerTitleView){
            if(object instanceof IIndicatorView){
                ((SimplePagerTitleView)textView).setText(((IIndicatorView) object).getTitleText());
            }else{
                ((SimplePagerTitleView)textView).setText(object.toString());
            }
        }

    }
}
